
Partial Class Modules_Questionnaires_Management_Questionnaire
    Inherits System.Web.UI.Page

    Dim tbStudyID As TextBox
    Dim rblVisitNumber As RadioButtonList
    Dim ddlInstrumentID, ddlStudyID As DropDownList
    Dim reqStudyID As RequiredFieldValidator
    Dim _Ensure As Boolean = True
    Dim _redirect As Boolean = False

    Protected Sub butSwitch_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        EnsureControlsLoaded()
        Dim butSwitch As Button = CType(sender, Button)
        If butSwitch.Text = "Switch to Existing List" Then
            butSwitch.Text = "Switch to TextBox"
            ddlStudyID.Visible = True
            ddlStudyID.SelectedValue = tbStudyID.Text
            tbStudyID.Visible = False
            reqStudyID.ControlToValidate = "ddlStudyID"
        Else
            butSwitch.Text = "Switch to Existing List"
            ddlStudyID.Visible = False
            tbStudyID.Visible = True
            reqStudyID.ControlToValidate = "tbStudyID"
        End If
    End Sub

    Protected Sub dvQuestionnaire_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewCommandEventArgs) Handles dvQuestionnaire.ItemCommand
        Select Case e.CommandName
            Case "cmdCancel"
                Response.Redirect("QuestionnaireManager.aspx")
        End Select
    End Sub

    Protected Sub dvQuestionnaire_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs) Handles dvQuestionnaire.ItemInserted
        Response.Redirect("QuestionnaireManager.aspx")
    End Sub

    Protected Sub dvQuestionnaire_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) Handles dvQuestionnaire.ItemInserting
        EnsureControlsLoaded()
        If ddlStudyID.Visible Then
            e.Values("StudyID") = ddlStudyID.SelectedValue
        Else
            e.Values("StudyID") = tbStudyID.Text
        End If
        e.Values("VisitNumber") = rblVisitNumber.SelectedValue
        e.Values("InstrumentID") = ddlInstrumentID.SelectedValue
    End Sub

    Protected Sub dvQuestionnaire_ItemUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdatedEventArgs) Handles dvQuestionnaire.ItemUpdated
        If _redirect Then
            Response.Redirect("QuestionnaireManager.aspx")
        End If
    End Sub

    Protected Sub dvQuestionnaire_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles dvQuestionnaire.ItemUpdating
        EnsureControlsLoaded()
        If ddlStudyID.Visible Then
            e.NewValues("StudyID") = ddlStudyID.SelectedValue
        Else
            e.NewValues("StudyID") = tbStudyID.Text
        End If
        e.NewValues("VisitNumber") = rblVisitNumber.SelectedValue
        e.NewValues("InstrumentID") = ddlInstrumentID.SelectedValue
    End Sub

#Region "Private Subs and Functions"

    Private Sub EnsureControlsLoaded()
        If _Ensure Then
            tbStudyID = dvQuestionnaire.FindControl("tbStudyID")
            rblVisitNumber = dvQuestionnaire.FindControl("rblVisitNumber")
            ddlInstrumentID = dvQuestionnaire.FindControl("ddlInstrumentID")
            ddlStudyID = dvQuestionnaire.FindControl("ddlStudyID")
            reqStudyID = dvQuestionnaire.FindControl("reqStudyID")
            _Ensure = False
        End If
    End Sub

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        With dvQuestionnaire
            If Session("questionnaire_questionnaireId") = "0" Then
                .ChangeMode(DetailsViewMode.Insert)
                .Fields(12).Visible = True
                lblHeader.Text = "QUESTIONNAIRE CREATOR"
            ElseIf Request.QueryString("mode") = "edit" Then
                .ChangeMode(DetailsViewMode.Edit)
                .Fields(11).Visible = True
                lblHeader.Text = "QUESTIONNAIRE EDITOR"
            Else
                .Fields(13).Visible = True
                lblHeader.Text = "QUESTIONNAIRE DETAIL"
            End If
        End With
    End Sub

    Protected Sub lbUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        _redirect = True
    End Sub
End Class
